package com.rk.controller;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.rk.model.User;
import com.rk.service.LoginService;


@Controller
public class CreateUserController {
	
	
	
	private LoginService loginService;

	public LoginService getLoginService() {
		return loginService;
	}


	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}


	private static final String USER_FORM = "user";
	private static final String CREATE_USER_JSP = "register";

	@RequestMapping(value="/createUser",method = RequestMethod.GET)        
	public String showForm(Map<String,User> model)
	{            
		User user = new User();
		model.put(USER_FORM, user);
		return CREATE_USER_JSP;      
	}  
	
	@RequestMapping(value="/createUser" , params="save" , method=RequestMethod.POST)
	public String createUser(User user ,BindingResult result,  Map<Object, Object> model, HttpSession session, Locale locale) throws Exception
	{
		if(result.hasErrors()){
			return CREATE_USER_JSP;
		}
		user = (User) model.get(USER_FORM);
		try{
			loginService.saveUser(user);
		}catch(Exception e){
			throw e;
		}
		return "login";

		
	}
	@RequestMapping(value="/createUser" , params="cancel" , method=RequestMethod.POST)
	public String processCancle()
	{
		return "login";
	}
	
	
	
}
